<?php
	require("libraries/student.php");
	class Category extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_category");
			$this->load->helper("form");
			$this->load->library("form_validation");
			$this->load->library("string");
		}
		public function index(){
			$config['base_url'] = base_url().'admin/category/index/';
			$config['total_rows'] = $this->model_category->count_all();
			$data['title'] = "Manage cotegories";
			$data['template'] = "category/list_cate";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['act'] = 5;
			$data['listcate']= $this->model_category->listcate($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function active(){
			$id   	= $this->uri->segment(4);
			$status	= $this->uri->segment(5);
			$db = array(
				"category_status" => $status 
			);
			$this->model_category->update_status($db,$id);
			redirect(base_url()."admin/category/");
		}
		public function ajax(){
			$limit = 10;
			$start = $this->uri->segment(4);
			$data = $this->model_category->listcago($limit,$start);
			$total = $this->model_category->count_all_cago();
			$this->Paging->page();
			echo "<table>";
			echo "<tr>";
			echo "<td class='title' width='30'>Stt</td>";
			echo "<td class='title' width='160'>Tên chuyên mục</td>";
			echo "<td class='title' width='160'>Thuộc chuyên mục</td>";	
			echo "<td class='title' width='40'>Sửa</td>";
			echo "<td class='title' width='40'>Xóa</td>";											
			echo "</tr>";
			$stt=0;
			foreach($data as $item){
				$stt++;
				echo "<tr>";
				echo "<td>$stt</td>";
				echo "<td>".$item['cago_name']."</td>";
				echo "<td>".$item['category_name']."</td>";
				echo "<td>Sửa</td>";
				echo "<td>Xóa</td>";
				echo "</tr>";	
			}
			echo "</table>";
			echo $this->Paging->listpage(5,$start,$limit);
		}
		public function add(){
		  	$data['title']= "Add category";
			$data['template'] = "category/add_cate";
			$data['data'] = "";
			$data['act'] = 5;
			if($this->input->post("ok") != ""){
				$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('category_name'))));
				$value = array(
						"category_name" => $this->input->post("category_name"),
						"category_rewrite" => $url,
						"category_order" => $this->input->post("category_order")
				);
				$this->model_category->add($value);
				redirect(base_url()."admin/category");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['title']= "Edit category";
			$data['template'] = "category/edit_cate";
			$data['data'] = "";
			$data['act'] = 5;
			$data['getdata'] = $this->model_category->getdata($id);
			if($this->input->post("ok") != ""){
				$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('category_name'))));
				$value = array(
							"category_name" => $this->input->post("category_name"),
							"category_rewrite" => $url,
							"category_order" => $this->input->post("category_order")
						);
				$this->model_category->update($value,$id);
				redirect(base_url()."admin/category/index");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$this->model_category->del($id);
			redirect(base_url()."admin/category/index");
		}
		public function createThumbnail($fileName){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/category/'.$fileName;
			$config['new_image'] = 'uploads/category/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = 160;
			$config['height'] = 130;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}